package t2.server;

import java.lang.reflect.InvocationTargetException;

import t2.client.rpc.RemoteAtCmd;
import t2.server.xbee.XBeePort;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.rapplogic.xbee.api.RemoteAtRequest;
import com.rapplogic.xbee.api.RemoteAtResponse;
import com.rapplogic.xbee.api.XBeeAddress64;
import com.rapplogic.xbee.api.XBeeException;
import com.rapplogic.xbee.util.ByteUtils;

public class RemoteAtCmdImpl extends RemoteServiceServlet implements RemoteAtCmd {

    private static final long serialVersionUID = 1449497710588079260L;

    @Override
    public String remoteATquery(int[] dest64, String command) {
        XBeeAddress64 addr64 = new XBeeAddress64(dest64);
        RemoteAtRequest transmission = new RemoteAtRequest(addr64, command);
        try {
            @SuppressWarnings("rawtypes")
            Class byteUtilsClass = Class.forName("com.rapplogic.xbee.util.ByteUtils");
            Object byteUtilsObj = byteUtilsClass.newInstance();


            RemoteAtResponse receipt = (RemoteAtResponse) XBeePort.getXBeePort().getXbee()
                    .sendSynchronous(transmission);
            Object returnedString = ATcmdMap.getInstance().getMap().get(command)
                    .invoke(byteUtilsObj, receipt.getValue());

            if (receipt.isOk()) {
                return returnedString.toString();
            }
        }
        catch (XBeeException e) {
            e.printStackTrace();
        }
        catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        catch (InstantiationException e) {
            e.printStackTrace();
        }
        catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        catch (IllegalArgumentException e) {
            e.printStackTrace();
        }
        catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return command;
    }


    @Override
    public String remoteATconfig(int[] dest64, String command, int param) {
        XBeeAddress64 addr64 = new XBeeAddress64(dest64);
        int[] param_arr = new int[] { param };
        RemoteAtRequest transmission = new RemoteAtRequest(addr64, command, param_arr);
        try {
            RemoteAtResponse receipt = (RemoteAtResponse) XBeePort.getXBeePort().getXbee()
                    .sendSynchronous(transmission);
            if (receipt.isOk()) {
                return dest64.toString() + " is configured " + command;
            } else {
                return "Problem configuring " + String.valueOf(param);
            }
        }
        catch (XBeeException e) {
            e.printStackTrace();
            return e.toString();
        }
    }

    @Override
    public String remoteATconfig(int[] dest64, String command, String param) {
        XBeeAddress64 addr64 = new XBeeAddress64(dest64);
        int[] param_arr = ByteUtils.stringToIntArray(param);
        RemoteAtRequest transmission = new RemoteAtRequest(addr64, command, param_arr);
        try {
            RemoteAtResponse receipt = (RemoteAtResponse) XBeePort.getXBeePort().getXbee()
                    .sendSynchronous(transmission);
            if (receipt.isOk()) {
                return dest64.toString() + " is configured " + command;
            } else {
                return "Problem configuring " + String.valueOf(param);
            }
        }
        catch (XBeeException e) {
            e.printStackTrace();
            return e.toString();
        }
    }
}